package com.zzsy.carosshopprojects.activitys.home.mainpage.charge;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.LinearLayout;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.activitys.home.mainpage.common.CollectionFragment;
import com.zzsy.carosshopprojects.adapter.ChoseOilAdapter;
import com.zzsy.carosshopprojects.base.BaseApplation;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.base.ConfigureFile;
import com.zzsy.carosshopprojects.util.StringUtils;
import com.zzsy.carosshopprojects.util.ViewBgUtil;
import com.zzsy.carosshopprojects.view.MyGridView;
import com.zzsy.carosshopprojects.view.MyNumberKeyboard;
import com.zzsy.carosshopprojects.view.keyboard.AmountInputView;

import butterknife.BindDimen;
import butterknife.BindView;

/**
 * 加油金额
 */
public class ChoseOilFragment extends BaseFragment {
    public static ChoseOilFragment newInstance() {
        ChoseOilFragment choseOilFragment = new ChoseOilFragment();
        return choseOilFragment;
    }

    @BindView(R.id.recyclerView)
    MyGridView recycleView;
    @BindView(R.id.amountInputView)
    AmountInputView amountInputView;
    @BindView(R.id.keyboard)
    MyNumberKeyboard numberKeyboard;
    @BindView(R.id.ly_input)
    LinearLayout ly_input;

    @BindDimen(R.dimen.p_25)
    int p_20;
    private ChoseOilAdapter choseOilAdapter;
    private ChoseOilMode choseOilMode;
    private String usedOil, productId;
    private String oilsPrice;
    private String productInfoId;

    @Override
    protected int initLayout() {
        return R.layout.fg_choseoil;
    }

    @Override
    protected void initView(View view) {
        initTitle(view, getResources().getString(R.string.joinOilAccont), getActivity());
        numberKeyboard.setKeyClickCallback(new MyNumberKeyboard.KeyClickCallback() {
            @Override
            public void onNumClick(String keyNum) {
                amountInputView.addText(keyNum + "");
            }

            @Override
            public void onDelClick() {
                amountInputView.delLast();
            }

            @Override
            public void onCleanClick() {
                amountInputView.clean();
            }

            @Override
            public void sure() {
                String amount = (String) amountInputView.getAmountText();
                if ("0.".equals(amount) || "0.0".equals(amount) || "0.00".equals(amount) || "0".equals(amount) || StringUtils.isEmpty(amount)) {
                    ToastUtils.showToast(BaseApplation.instance().getApplicationContext(), getContext().getApplicationContext().getResources().getString(R.string.input_money));
                    return;
                }
                if (StringUtils.isEmpty(usedOil)) {
                    ToastUtils.showToast(BaseApplation.instance().getApplicationContext(), getContext().getApplicationContext().getResources().getString(R.string.chose_oil));
                    return;
                }
                startWithPop(CollectionFragment.newInstance(amount, usedOil, productId, oilsPrice, productInfoId));
            }
        });

        ly_input.setBackground(ViewBgUtil.setWidthBg(15, 1, Color.parseColor(ConfigureFile.ColorWhite), Color.parseColor("#d7d7d7")));
    }

    @Override
    protected void initData(Context mContext) {
        choseOilMode = getViewModelProvider().get(ChoseOilMode.class);
        choseOilAdapter = new ChoseOilAdapter(getContext(), null, 0, false);
        recycleView.setAdapter(choseOilAdapter);
        recycleView.setOnItemClickListener((adapterView, view, position, l) -> {
            choseOilAdapter.setPostion(position);
            usedOil = choseOilAdapter.getListData().get(position).getPnumber();
            productId = choseOilAdapter.getListData().get(position).getProductId();
            oilsPrice = choseOilAdapter.getListData().get(position).getPrice();
            productInfoId = choseOilAdapter.getListData().get(position).getId();
        });
    }

    @Override
    protected void loadMethod() {
        showListData();
    }

    private void showListData() {
        choseOilMode.getLiveData().observe(this, MenuBeans -> {
            choseOilAdapter.setListData(MenuBeans);
            choseOilAdapter.notifyDataSetChanged();
        });
        choseOilMode.getData(getContext());
    }


}
